package com.example.flex.controller.generator;

import com.example.flex.domain.generator.DataBaseEntity;
import com.example.flex.service.generator.DatabaseService;
import com.example.flex.utils.response.BaseResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;

/**
 * @Author: 不是菜鸡爱编程
 * @Date: 2023/10/25
 * @Description：
 */
@Slf4j
@RestController
@Api(tags = "连接数据接口")
@RequestMapping("database")
public class DataBaseController {
    @Autowired
    private DatabaseService databaseService;

    /**
     * 查看数据库列表
     *
     * @return {@link BaseResponse}<{@link ?}>
     */
    @ApiOperation("查看数据库列表")
    @GetMapping("/list")
    public BaseResponse<?> list() {
        return BaseResponse.success(databaseService.listDatabaseName(), "数据库列表获取成功");
    }

    /**
     * 连接数据库
     *
     * @param dataBase 数据库
     * @return {@link BaseResponse}<{@link ?}>
     */
    @ApiOperation("连接数据库")
    @PostMapping("/connect")
    public BaseResponse<?> connect(@RequestBody @ApiParam("数据库连接实体类") DataBaseEntity dataBase) {
        return databaseService.connect(dataBase);
    }

    /**
     * 配置代码生成包路径
     *
     * @param packageName 包名
     * @return {@link BaseResponse}<{@link ?}>
     * @throws SQLException sqlexception异常
     */
    @ApiOperation("配置代码生成包路径")
    @GetMapping("/config")
    public BaseResponse<?> config(@RequestParam("packageName") @ApiParam("代码生成包路径") String packageName) throws SQLException {
        return databaseService.config(packageName);
    }
}
